<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    //Configurando AutoLoader
    protected function _initAutoLoader() {
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace("Eagle");
        return $autoloader;
    }

    //Habilitar protocolo REST
    protected function _initRestRoute() {
        $this->bootstrap("frontController");
        $frontController = Zend_Controller_Front::getInstance();
        $restRoute = new Zend_Rest_Route($frontController);
        $frontController->getRouter()->addRoute("default", $restRoute);
    }

    //Habilitar Service Layer
    protected function _initResourceLoader() {
        $this->_resourceLoader->addResourceType("service", "services", "Service");
    }

    //Habilitar Helper
    protected function _initHelpers() {
        Zend_Controller_Action_HelperBroker::addPrefix("Eagle_Helper");
    }

    //Habilitar Plugins
//    protected function _initPlugins() {
//        $bootstrap = $this->getApplication();
//        if ($bootstrap instanceof Zend_Application) {
//            $bootstrap = $this;
//        }
//        $bootstrap->bootstrap('FrontController');
//        $front = $bootstrap->getResource('FrontController');
//
//        $front->registerPlugin(new Eagle_Plugin_CheckAuth());
//    }

    protected function _initZFDebug() {
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace('ZFDebug');
        $options = array('plugins' => array(
                'Variables', 'File' => array(
                    'base_path' => APPLICATION_PATH),
                'Memory',
                'Time',
                'Registry',
                'Exception',
                'Cache' => array(
                    'backend' => APPLICATION_PATH . '/cache'
                )
            )
        );
        # Instantiate the database adapter and setup the plugin.
        # Alternatively just add the plugin like above and rely on the autodiscovery feature. 
        if ($this->hasPluginResource('db')) {
            $this->bootstrap('db');
            $db = $this->getPluginResource('db')->getDbAdapter();
            $options['plugins']['Database']['adapter'] = $db;
        }
        # Setup the cache plugin
        if ($this->hasPluginResource('cache')) {
            $this->bootstrap('cache');
            $cache = $this - getPluginResource('cache')->getDbAdapter();
            $options['plugins']['Cache']['backend'] = $cache->getBackend();
        }
        $debug = new ZFDebug_Controller_Plugin_Debug($options);
        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $frontController->registerPlugin($debug);
    }

}

